﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TTEngine
{
    public class DamageExpression
    {
        //constructor
        public DamageExpression(string s)
        {
            string[] tokens = s.Split('=');
            DamageDice = tokens[0];
            DamageType = tokens[1];
        }

        //public properties
        public string DamageDice { get; set; }
        public string DamageType { get; set; }

        //get a damage value
        public int DamageValue
        {
            get
            {
                if (DamageDice.Contains('d'))
                {
                    return RollDice(1, 'd');
                }
                else if (DamageDice.Contains('z'))
                {
                    return RollDice(0, 'z');
                }
                else
                    return Int32.Parse(DamageDice);
            }
        }

        //private members
        private int RollDice(int min, char splitChar)
        {
            string[] tokens = DamageDice.Split(splitChar);
            int dice = Int32.Parse(tokens[0]);
            int sides = Int32.Parse(tokens[1]);
            int sum = 0;
            while (dice > 0)
            {
                sum += Globals.RandomIntFromTo(min, sides);
                dice--;
            }
            return sum;
        }

        //return a random damage description
        public string DamageDescription
        {
            get
            {
                switch (DamageType)
                {
                    case "b":
                        {
                            return BashNames[Globals.RandomIntFromTo(0, BashNames.Length - 1)];
                        }
                    case "p":
                        {
                            return PierceNames[Globals.RandomIntFromTo(0, PierceNames.Length - 1)];
                        }
                    case "s":
                        {
                            return SlashNames[Globals.RandomIntFromTo(0, SlashNames.Length - 1)];
                        }
                    case "f":
                        {
                            return FireNames[Globals.RandomIntFromTo(0, FireNames.Length - 1)];
                        }
                    default:
                        return "attacks";
                }
            }
        }

        //attack names
        private static string[] PierceNames = { "stabs", "impales", "pierces", "skewers", "gouges", "wounds" };
        private static string[] BashNames = { "bashes", "pounds", "smites", "smacks", "pummels", "crushes" };
        private static string[] SlashNames = { "slashes", "cuts", "rips", "tears", "gouges", "wounds" };
        private static string[] FireNames = { "burns", "immolates", "scorches" };
    }
}
